﻿using System;
using System.Collections.Generic;
using System.Threading;
using D3;
using D3.Pathing;
using KCBF.Modules.AI;
using KCBF.Modules.Config;
using NYTBD;
using NYTBD.Bots;
using NYTBD.Common;
using NYTBD.Modules;

namespace NYTBD
{
    public class NYTBD
    {

        private readonly Logger _logger;

        private readonly Thread _thread;

        private int _gameCounter;

        private readonly List<IBot> bots = new List<IBot>(); 

        public NYTBD()
        {
            
            _logger = new Logger("NYTBD");

            InitStatics();

            bots.Add(new AShatteredCrown());

            Game.OnTickEvent += Game_OnTickEvent;

            _thread = new Thread(ExecutingThread);

        }

        public static NYTBD Instance { get; set; }

        public void InitStatics()
        {
            Hero.Init();
            Multiverse.init();
            Squanto.Init();
            _Game.Init();
            Blackboard.SetGlobal("Running","true");
        }

        private void Game_OnTickEvent(EventArgs e)
        {
            if (Game.Ingame)
            {
                if (Me.WorldId != 0 && Me.Life < 1)
                {
                    Console.WriteLine("I died! Exitting game");
                    Pdp11.ExitGame();
                }
            }
        }

        public void Start()
        {
            _thread.Start();
        }

        public void Stop()
        {
            _thread.Abort();
        }

        private void ExecutingThread()
        {
            
            //if (Game.Ingame) PDP11.ExitGame();
            Thread.Sleep(2000);

            while (true)
            {
                int runStart = Environment.TickCount;

                foreach (var bot in bots)
                {


                    //PDP11.ChangeQuest((uint) bot.GetQuest());
                    //PDP11.EnterGame();
                    bot.Reset();
                    bot.Execute();
                    //PDP11.ExitGame();
                    Thread.Sleep(3000);
                }

                _logger.Log("Mira Run Took: " + (Environment.TickCount - runStart)/1000 + " seconds");
            }
        }
    }
}